import axios from "axios";
import { Message } from 'element-ui';
import router from '../router'

// axios 请求拦截器
axios.interceptors.request.use(config=> {
    //若sessionStorage中存在tokenStr
    let tokenString = window.sessionStorage.getItem('tokenStr');
    if (tokenString) {
        config.headers['Authorization'] = tokenString;
    }
    return config;
}, error => {
    console.log(error);
});

//使用axios拦截器 拦截响应请求进行集中处理
axios.interceptors.response.use(success => {
    //业务逻辑错误
    if (success.status && success.status == 200) {
        if (success.data.code == 500 || success.data.code == 401) {
            Message.error({message: success.data.message});
            return;
        }
        if (success.data.message) {
            Message.success(success.data.message)
        }
    }
    return success.data;
}, error => {
    //接口调用错误
    if (error.response.code == 500) {
        Message.error("服务器没了o(╯□╰)o");
    } else if (error.response.code == 403) {
        Message.error("权限不足！");
    } else if (error.response.code == 401) {
       Message.error("请先登录");
       router.replace('/');
    } else {
        Message.error("未知错误！");
    }
    return;
})

//前置路径
let base = '';
//封装请求
export const postRequest = (url, params) => {
    return axios({
        method:'post',
        url: `${base}${url}`,
        data:params
    });
}

//put请求
export const putRequest = (url, params) => {
    return axios({
       method: 'put',
       url: `${base}${url}`,
       data: params
    });
}

//get请求
export const getRequest = (url, params) => {
    return axios({
        method: 'get',
        url: `${base}${url}`,
        data: params
    });
}

//delete请求
export const deleteRequest = (url, params) => {
    return axios({
        method: 'delete',
        url: `${base}${url}`,
        data: params
    });
}